package kr.hkit.summart_java.serviceUI;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingConstants;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumnModel;
import javax.swing.table.TableModel;

import kr.hkit.summart_java.dao.DaoInterface;
import kr.hkit.summart_java.erp.dto.CodeTitle;
import kr.hkit.summart_java.erp.dto.Employee;

public class PanelCodeTitleUI2 extends JPanel implements ActionListener {
	private JTable table;
	private JButton btnAdd2;
	private DaoInterface<CodeTitle> dao2;
	public Object btnlist;

	/**
	 * Create the panel.
	 */
	public PanelCodeTitleUI2() {

		initialize();
		tableCellAlginAndWidth();
	}
	
	private void tableCellAlginAndWidth() {
		//가운데 정렬
		DefaultTableCellRenderer dtcrCenter = new DefaultTableCellRenderer();
		dtcrCenter.setHorizontalAlignment(SwingConstants.CENTER);
		
		//우측 정렬
		DefaultTableCellRenderer dtcrRight = new DefaultTableCellRenderer();
		dtcrRight.setHorizontalAlignment(SwingConstants.RIGHT);
		
		TableColumnModel cModel = table.getColumnModel();
		
		int columnCount= cModel.getColumnCount();//컬럼의 개수
		
		cModel.getColumn(0).setPreferredWidth(70);
		cModel.getColumn(0).setCellRenderer(dtcrCenter);
		
		cModel.getColumn(1).setPreferredWidth(70);
		cModel.getColumn(1).setCellRenderer(dtcrCenter);

	}

	private void initialize() {
		setLayout(new BorderLayout(0, 0));
		
		JPanel panel_Top = new JPanel();
		add(panel_Top, BorderLayout.NORTH);
		
		btnAdd2 = new JButton("추가");
		btnAdd2.addActionListener(this);
		panel_Top.add(btnAdd2);
		
		JPanel panel_Bottom = new JPanel();
		add(panel_Bottom, BorderLayout.SOUTH);
		
		final JLabel lblSelected = new JLabel("New label");
		panel_Bottom.add(lblSelected);
		
		JScrollPane scrollPane = new JScrollPane();
		add(scrollPane, BorderLayout.CENTER);
		
		String[] colNames={"사원번호", "사원명"};
		TableModel model = new DefaultTableModel(colNames, 0){
			@Override
			public Class<?> getColumnClass(int columnIndex) {
				return getValueAt(0, columnIndex).getClass();
			}
		};
		
		table = new JTable(model);
		table.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				super.mouseClicked(e);
				int row = table.getSelectedRow();
			
				StringBuilder sb = new StringBuilder();
				for(int i=0; i<table.getColumnCount(); i++){
					sb.append(table.getModel().getValueAt(row, i)+" ");
				}
				lblSelected.setText(sb.toString());
			}
			
		});
		
		scrollPane.setViewportView(table);
		
	}

	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == btnAdd2) {
			actionPerformedBtnAdd2(e);
		}
	}
	protected void actionPerformedBtnAdd2(ActionEvent e) {
		DefaultTableModel model = (DefaultTableModel) table.getModel();
		ArrayList<CodeTitle> list2 = new ArrayList<>();
		try {
			dao2.listItems();
			list2 = dao2.listItems();
			for(int i =0;i<dao2.listItems().size();i++){
			Object[] newRow = {
					list2.get(i).getTitleNo(),
					list2.get(i).getTitleName()

			};
			
			model.addRow(newRow);
			}
			
			JOptionPane.showMessageDialog(this, "검색되었습니다.");
		} catch (SQLException e1) {
			JOptionPane.showMessageDialog(this, "실패.");
			
		}
		
	}
	public void setDao(DaoInterface<CodeTitle> dao) {
		this.dao2 = dao;
	}
}




